package com.zhouyu.service;

import com.spring.BeanPostProcessor;
import com.spring.Component;
import com.spring.ZhouyuValue;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

@Component
public class ZhouyuValuePostProcessor implements BeanPostProcessor {
	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName) {

		for (Field field : bean.getClass().getDeclaredFields()) {
			if (field.isAnnotationPresent(ZhouyuValue.class)){
				field.setAccessible(true);
				try {
					field.set(bean,field.getAnnotation(ZhouyuValue.class).value());
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				}
			}
		}

		return bean;
	}

	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName) {
		return bean;
	}
}
